﻿using System;
using WiMo.Games.Drawables;
using System.Drawing;

namespace WiMo.Games.WM5.Drawables
{
    public class Rectangle : SpriteShapeBase
    {       
        public Rectangle()
        {
            FillColor = Color.Transparent;
            BorderColor = Color.Black;
            BorderWidth = 1;
            Origin = Origin.Center;
            Force = Vector2.Zero;
            ShapeType = ShapeType.Rectangle;
            CollisionShape = CollisionShapes.Rectangle;
        }

        public override WiMo.Games.Rectangle Rect 
        {
            get { return new WiMo.Games.Rectangle(Position.Point.X - (Size.Width >> 1), Position.Point.Y - (Size.Height >> 1), Size.Width, Size.Height); }
            set 
            { 
                Size = new Size(value.Width, value.Height);
                Position = new PointF(Rect.Left + Size.Width >> 1, Rect.Top + Size.Height >> 1);
            }
        }
       
        public override void Render(IDisplay display)
        {
            if (DrawInformation == null)
            {
                DrawInformation = new Bitmap(Size.Width, Size.Height);
                using (var gr = Graphics.FromImage(DrawInformation as Bitmap))
                {
                    if (FillColor != Color.Transparent)
                        using (var sb = new SolidBrush(FillColor.ToDrawingColor()))
                        {
                            gr.FillRectangle(sb, 0, 0, Size.Width, Size.Height);
                        }

                    if (BorderColor != Color.Transparent)
                        using (var pen = new Pen(BorderColor.ToDrawingColor(), Convert.ToSingle(BorderWidth)))
                        {
                            gr.DrawRectangle(pen, 0, 0, Size.Width, Size.Height);
                        }
                }
            }

            display.Render(this);
        }

        public override void Dispose()
        {

        }
    }
}
